package services;

import com.opensymphony.xwork2.ActionContext;
import domain.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 09/08/12
 * Time: 16:06
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ProtocolCRUDService{

    private ProtocoloDAO protocoloDAO;
    private ProtocoloCentroDAO protocoloCentroDAO;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;
    private TherapeuticAreaCRUDService therapeuticAreaCRUDService;
    private SpecialtyDAO specialtyDAO;
    private DrogaDAO drogaDAO;

    public ProtocolCRUDService() {
    }

    @Autowired
    public ProtocolCRUDService(ProtocoloDAO protocoloDAO, ProtocoloCentroDAO protocoloCentroDAO, ProtocoloCompromisopaisService protocoloCompromisopaisService, TherapeuticAreaCRUDService therapeuticAreaCRUDService, SpecialtyDAO specialtyDAO, DrogaDAO drogaDAO) {
        this.protocoloDAO = protocoloDAO;
        this.protocoloCentroDAO = protocoloCentroDAO;
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
        this.therapeuticAreaCRUDService = therapeuticAreaCRUDService;
        this.specialtyDAO = specialtyDAO;
        this.drogaDAO = drogaDAO;
    }

    public <V extends Criteria> List<Protocolo> list(V criteria) {
        List<Protocolo> list = protocoloDAO.findAll();
        Iterator<Protocolo> it = list.iterator();
        while(it.hasNext()){
            if(!criteria.isIn(it.next())){
                it.remove();
            }
        }
        return list;
    }

    public List<Protocolo> getProtocols(){
        return protocoloDAO.findAll();
    }

    public Protocolo findById(String id){
        return protocoloDAO.findById(id);
    }

    public ProtocoloCompromisopais recoverProtocoloCompromisopais() throws NoProtocoloCompromisopaisSelectedException {
        Map session = ActionContext.getContext().getSession();

        ProtocoloCompromisopaisId protocoloCompromisopaisId = new ProtocoloCompromisopaisId();
        protocoloCompromisopaisId.setProtocoloid((String)session.get("protocoloId"));
        protocoloCompromisopaisId.setCompromisopaisid((Integer)session.get("compromisopaisid"));

        ProtocoloCompromisopais aux = protocoloCompromisopaisService.findById(protocoloCompromisopaisId);

        if(aux == null){
            throw new NoProtocoloCompromisopaisSelectedException();
        }else{
            return aux;
        }
    }

    public Protocolo recoverProtocolo() throws NoProtocoloCompromisopaisSelectedException {
        Map session = ActionContext.getContext().getSession();

        Protocolo aux = (Protocolo) session.get("protocolo");

        if(aux == null){
            throw new NoProtocoloCompromisopaisSelectedException();
        }else{
            return aux;
        }
    }

    public void setProtocoloCompromisoPais(String protocoloId, int compromisopaisId){
        Map session = ActionContext.getContext().getSession();
        session.put("protocolo", protocoloCompromisopaisService.getByProtocoloIdCompromisopaisId(protocoloId, compromisopaisId));
    }

    public void setProtocolo(String protocoloId){
        Map session = ActionContext.getContext().getSession();
        session.put("protocolo", protocoloDAO.findById(protocoloId));
    }

    public Protocolo save(Protocolo protocolo) {
        ArrayList<Droga> drogas = new ArrayList<Droga>(0);

        Iterator<Droga> it = protocolo.getDrogas().iterator();
        while(it.hasNext()){
            Droga droga = it.next();
            if(droga != null && droga.getId() != null && droga.getId().intValue() != -1){
                drogas.add(drogaDAO.findById(droga.getId()));
            }
        }

        protocolo.setDrogas(drogas);

        for(Droga d : protocolo.getDrogas()){
            d.getProtocolos().add(protocolo);
        }
        if(protocolo.getTherapeuticArea() != null && protocolo.getTherapeuticArea().getId() != -1)
            protocolo.setTherapeuticArea(therapeuticAreaCRUDService.findById(protocolo.getTherapeuticArea().getId().intValue()));
        if(protocolo.getSpeciality() != null && protocolo.getSpeciality().getId() != -1)
            protocolo.setSpeciality(specialtyDAO.findById(protocolo.getSpeciality().getId()));
        return protocoloDAO.merge(protocolo);
    }

    public ArrayList<Centro> findSitesByProtocol(ProtocoloCompromisopais protocoloCompromisopais) {
        ArrayList<Centro> centros = new ArrayList<Centro>(0);
        ArrayList<ProtocoloCentro> lista = (ArrayList<ProtocoloCentro>) protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais);
        for(ProtocoloCentro p : lista){
            centros.add(p.getCentro());
        }
        return centros;
    }
}
